package com.czy.owner.ui;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Window;
import android.view.WindowManager;

import com.czy.owner.R;
import com.czy.owner.db.UserHelper;
import com.czy.owner.global.Constants;
import com.czy.owner.entity.AdvertisementEntity;
import com.czy.owner.entity.WebViewRequestParam;
import com.czy.owner.utils.AdvertisementManager;
import com.czy.owner.utils.MyLog;
import com.czy.owner.widget.SplashView;
import com.easemob.cases.MessageHelper;
import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;

import org.xutils.common.Callback;
import org.xutils.x;

import java.io.File;

import static com.czy.owner.entity.WebViewRequestParam.REQUEST_GET;
import static com.czy.owner.entity.WebViewRequestParam.REQUEST_POST;


/**
 * Created by wangyang on 2017/2/6 0006.
 * 广告
 */

public class AdvertisingActivity extends BaseActivity {

    private final String TAG = getClass().getSimpleName();

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_advertising;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        final AdvertisementEntity advertisementEntity = AdvertisementManager.getInstance().getAdvertisementInformation(AdvertisingActivity.this, AdvertisementManager.AD_TYPE_ADV_PAGE);
        if (advertisementEntity != null) {
            x.image().loadFile(advertisementEntity.getPicUrl() + "-1920%7C1080", null, new Callback.CacheCallback<File>() {
                private boolean hasCache = false;
                private boolean isDissmiss = false;

                @Override
                public boolean onCache(File result) {
                    MyLog.e("yang", "onCache");
                    if (result != null && result.exists()) {
                        hasCache = true;
                        SplashView.showSplashViewByPath(AdvertisingActivity.this, 3, result.getPath(), new SplashView.OnSplashViewActionListener() {
                            @Override
                            public void onSplashImageClick(String actionUrl) {
                                if (!TextUtils.isEmpty(advertisementEntity.getLinkUrl())) {
                                    if (advertisementEntity.getLinkUrl().contains("/own/shop/doubleEleven")) {
                                        MyLog.e("yang", "1111111111111");
                                        WebViewRequestParam webViewRequestParam = new WebViewRequestParam(advertisementEntity.getLinkUrl());
                                        webViewRequestParam.setRequestMethod(REQUEST_POST);
                                        Intent intent = new Intent(AdvertisingActivity.this, WebViewActivity.class);
                                        intent.putExtra(Constants.TRANSMISSION_CONTENT_KEY, webViewRequestParam);
                                        intent.putExtra("type", "doubleEleven");
                                        startActivityForResult(intent, 848);
                                    } else {
                                        if (advertisementEntity.getStatus().equals("enable") && advertisementEntity.getLinkType() == 2) {
                                            MyLog.e("yang", "22222222222");
                                            isDissmiss = true;
                                            WebViewRequestParam webViewRequestParam = new WebViewRequestParam(advertisementEntity.getLinkUrl());
                                            webViewRequestParam.setRequestMethod(REQUEST_GET);
                                            Intent intent = new Intent(AdvertisingActivity.this, WebViewActivity.class);
                                            intent.putExtra(Constants.TRANSMISSION_CONTENT_KEY, webViewRequestParam);
                                            intent.putExtra("title", advertisementEntity.getTitle());
                                            startActivityForResult(intent, 848);
                                        }

                                    }

                                }
                            }

                            @Override
                            public void onSplashViewDismiss(boolean initiativeDismiss) {
                                MyLog.e("yang", "onSplashViewDismiss");
                                if (!isDissmiss) {
                                    intoMainActivity();
                                }

                            }
                        });
                    }
                    return true;
                }

                @Override
                public void onSuccess(File result) {
                    MyLog.e("yang", "onSuccess");
                    if (hasCache) {
                        return;
                    }
                    if (result != null && result.exists()) {
                        SplashView.showSplashViewByPath(AdvertisingActivity.this, 3, result.getPath(), new SplashView.OnSplashViewActionListener() {
                            @Override
                            public void onSplashImageClick(String actionUrl) {
                                if (!TextUtils.isEmpty(advertisementEntity.getLinkUrl())) {
                                    if (advertisementEntity.getLinkUrl().contains("/own/shop/doubleEleven")) {
                                        MyLog.e("yang", "1111111111111");
                                        WebViewRequestParam webViewRequestParam = new WebViewRequestParam(advertisementEntity.getLinkUrl());
                                        webViewRequestParam.setRequestMethod(REQUEST_POST);
                                        Intent intent = new Intent(AdvertisingActivity.this, WebViewActivity.class);
                                        intent.putExtra(Constants.TRANSMISSION_CONTENT_KEY, webViewRequestParam);
                                        intent.putExtra("type", "doubleEleven");
                                        startActivityForResult(intent, 848);
                                    } else {
                                        if (advertisementEntity.getStatus().equals("enable") && advertisementEntity.getLinkType() == 2) {
                                            MyLog.e("yang", "22222222222");
                                            isDissmiss = true;
                                            WebViewRequestParam webViewRequestParam = new WebViewRequestParam(advertisementEntity.getLinkUrl());
                                            webViewRequestParam.setRequestMethod(REQUEST_GET);
                                            Intent intent = new Intent(AdvertisingActivity.this, WebViewActivity.class);
                                            intent.putExtra(Constants.TRANSMISSION_CONTENT_KEY, webViewRequestParam);
                                            intent.putExtra("title", advertisementEntity.getTitle());
                                            startActivityForResult(intent, 848);
                                        }

                                    }
                                }
                            }

                            @Override
                            public void onSplashViewDismiss(boolean initiativeDismiss) {
                                MyLog.e("yang", "onSplashViewDismiss");
                                if (!isDissmiss) {
                                    intoMainActivity();
                                }
                            }
                        });
                    }

                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    ex.printStackTrace();
                    intoMainActivity();
                }

                @Override
                public void onCancelled(CancelledException cex) {
                }

                @Override
                public void onFinished() {
                }
            });
        } else {
            intoMainActivity();
        }

        //任意处调用网络图片地址
        //SplashView.updateSplashData(this, "111");
    }

    @Override
    protected void updateViews() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 去掉窗口标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        // 隐藏状态栏,全屏显示
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        super.onCreate(savedInstanceState);
    }

    //主页
    private void intoMainActivity() {
        if (UserHelper.getInstance().isLogined()) {
            MyLog.i(TAG, "TRUE");
            if (UserHelper.getInstance().getOwnerInfoModel(AdvertisingActivity.this).getBindStoreId() != 0) {
                MessageHelper.getInstance().reset();
                EMClient.getInstance().login(UserHelper.getInstance().getUserInfoModel(this).getImKey(), UserHelper.getInstance().getUserInfoModel(this).getImPasswd(), new EMCallBack() {//回调
                    @Override
                    public void onSuccess() {
                        MessageHelper.getInstance().getUserProfileManager().setCurrentUser(UserHelper.getInstance().getUserInfoModel(getApplicationContext()).getNickName(),
                                UserHelper.getInstance().getUserInfoModel(getApplicationContext()).getUserLogo());

                        EMClient.getInstance().groupManager().loadAllGroups();
                        EMClient.getInstance().chatManager().loadAllConversations();

                        MyLog.d("King", "登录聊天服务器成功！" + UserHelper.getInstance().getUserInfoModel(AdvertisingActivity.this).getImKey() + "|" + UserHelper.getInstance().getUserInfoModel(AdvertisingActivity.this).getImPasswd());
                        startActivity(new Intent(AdvertisingActivity.this, MainActivity.class));
                        finish();
                        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                    }

                    @Override
                    public void onProgress(int progress, String status) {

                    }

                    @Override
                    public void onError(int code, String message) {
                        MyLog.d("King", "登录聊天服务器失败！" + code + "|" + message);
                        startActivity(new Intent(AdvertisingActivity.this, MainActivity.class));
                        finish();
                        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                    }
                });


            } else {
                startActivity(new Intent(this, MainActivity.class));
                finish();
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            }

        } else {
            MyLog.i(TAG, "FALSE");
            startActivity(new Intent(this, MainActivity.class));
            finish();
            overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);


        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 848) {
            intoMainActivity();
        }
    }
}
